package com.htjx.mall.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.htjx.mall.commons.mappers.AlipayTradeMapper;
import com.htjx.mall.commons.models.AlipayTrade;
import com.htjx.mall.commons.models.enums.SeqType;

@Service
public class AlipayTradeService {
	@Autowired
	private AlipayTradeMapper alipayTradeMapper; 
	@Autowired
	private SequenceManager sequenceManager;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	public long insertOne(final AlipayTrade alipayTrade) {
		long[] seq = sequenceManager.buildBatchSeq(SeqType.AlipayTrade, 1);
		alipayTrade.setId(seq[0]);
		
		return transactionTemplate.execute(new TransactionCallback<Long>() {

			@Override
			public Long doInTransaction(TransactionStatus status) {
				// TODO Auto-generated method stub
				alipayTradeMapper.insertOne(alipayTrade);
				return alipayTrade.getId();
			}
		});
		
//		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
//			@Override
//			protected void doInTransactionWithoutResult(TransactionStatus status) {
//				alipayTradeMapper.insertOne(alipayTrade);
//			}
//		});
	}
}
